home *** CD-ROM | disk | FTP | other *** search
- /*
- * Main.c
- *
- * Robert Dierkes, November 11, 1993
- *
- * Change History:
- *
- * 11/93 ??? New
- * 4/96 bob Updated #includes to support changed GX Library names.
- * Changed boolean to Boolean.
- * Added the copyright info.
- *
- *
- * © Apple Computer, Inc. 1990 - 1996 All rights reserved
- *
- */
-
- /*------------------*/
- /* Include Files */
- /*------------------*/
- #include <GestaltEqu.h>
- #include <QuickDraw.h>
- #include <Types.h>
- #include <Fonts.h>
- #include <Windows.h>
- #include <Memory.h>
- #include <LowMem.h>
- #include <Dialogs.h>
-
- #define debugging
-
- #include <GXEnvironment.h>
- #include "GraphicsLibraries.h"
- #include <GXErrors.h>
-
- #include "Object.h"
- #include "ViewPorts.h"
- #include "Main.h"
-
-
- /*----------------------*/
- /* Global Declarations */
- /*----------------------*/
- gxGraphicsClient gClient;
- Boolean gQuitApp;
- Handle ghMainMenu;
- Rect gDragRect;
- WindowPtr gWindow;
- Boolean gDoAnimation,
- gCyclePanes;
- long gNextCycle;
-
-
- /*------------------------------*/
- /* External Declarations */
- /*------------------------------*/
- extern Boolean gIsSquare;
- extern Boolean gShowFrame;
- extern Boolean gDoDither;
- extern Boolean gDoHalftone;
- extern long gPanesPerSide;
-
-
- long GetNextCycle (long *pNextCycle);
- long
- GetNextCycle (long *pNextCycle)
- {
- long now;
-
- if ((now = TickCount ()) > *pNextCycle)
- {
- *pNextCycle = now + kCycleLength;
- return (true);
- }
- else
- return (false);
-
- } /* GetNextCycle */
-
-
- Boolean
- InitApp (void)
- {
- long response;
- gxGraphicsError grfxError;
-
- if (Gestalt (gestaltGraphicsVersion, &response)) {
- DebugStr ("\pQuickDraw GX is not installed");
- return false;
- }
-
- if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
- DebugStr ("\pNewGraphicsClient failed");
- return false;
- }
-
- /* Initialize Skia */
- GXEnterGraphics ();
-
- GXSetValidation (gxNoValidation);
- SetGraphicsLibraryErrors ();
- SetGraphicsLibraryNotices();
- InitCommonColors ();
-
- GXIgnoreGraphicsNotice (disposed_dead_caches);
-
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- InitDialogs (nil);
- InitCursor ();
-
- return true;
-
- } /* InitApp */
-
-
- void
- ExitApp (void)
- {
- if (gWindow)
- DisposeWindow (gWindow);
-
- DisposeCommonColors ();
-
- GXPopGraphicsNotice (/*disposed_dead_caches*/);
-
- GXExitGraphics ();
- if (gClient)
- GXDisposeGraphicsClient (gClient);
-
- } /* ExitApp */
-
-
- void
- InitializeMenus (void)
- {
- ghMainMenu = GetNewMBar (menuBarRsrcID);
- SetMenuBar (ghMainMenu);
- AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
- DrawMenuBar ();
-
- if (gDoAnimation) EnableItem (GetMHandle (windowMenuRsrcID), itemCycle);
- else DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
- CheckItem (GetMHandle (objectMenuRsrcID), gIsSquare ? itemSquare : itemOval, true);
- CheckItem (GetMHandle (objectMenuRsrcID), itemDither, gDoDither);
- CheckItem (GetMHandle (objectMenuRsrcID), itemHalftone, gDoHalftone);
- CheckItem (GetMHandle (windowMenuRsrcID), itemShowFrame, gShowFrame);
- CheckItem (GetMHandle (windowMenuRsrcID), itemCycle, gCyclePanes);
- CheckItem (GetMHandle (windowMenuRsrcID), gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes, true);
-
- } /* InitializeMenus */
-
-
- WindowPtr
- InitializeWindow (void)
- {
- WindowPtr pWindow;
- Rect bounds;
- short totalMargin;
- Point newPosition;
- GDHandle hGDevice;
-
- pWindow = GetNewCWindow (windowRsrcID, nil, kWindowOnTop);
- if (pWindow == nil)
- return (nil);
-
- SetPort (pWindow);
- GXIgnoreGraphicsNotice (transform_already_set);
- SetDefaultViewPort (GXNewWindowViewPort (pWindow));
- GXPopGraphicsNotice ();
-
- /* Find the deepest screen */
- SetRect (&bounds, -32767, -32767, 32767, 32767);
- hGDevice = GetMaxDevice (&bounds);
- bounds = (**hGDevice).gdRect;
-
- totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
- newPosition.h = bounds.left + (totalMargin >> 1);
-
- totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
- newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? GetMBarHeight() : 0)) >> 1);
-
- MoveWindow (pWindow, newPosition.h, newPosition.v, true);
- ShowWindow (pWindow);
-
- gDragRect = qd.screenBits.bounds;
- return (pWindow);
-
- } /* InitializeWindow */
-
-
- void
- DoContentClick (EventRecord *pEvent, WindowPtr theWindow)
- {
- if (theWindow == nil)
- return;
-
- if (theWindow == FrontWindow ())
- {
- if (GetWindowKind (theWindow) == noGrowDocProc)
- ;
- }
- else
- {
- SetPort (theWindow);
- SelectWindow (theWindow);
- }
-
- } /* DoContentClick */
-
-
- void
- DoMenuCommand (long menuResult)
- {
- short menuID,
- itemNumber;
-
- if (! menuResult)
- return;
-
- menuID = menuResult>>16;
- itemNumber = menuResult & 0xFFFF;
-
- switch (menuID)
- {
- case appleMenuRsrcID:
- switch (itemNumber)
- {
- case itemAbout:
- Alert (aboutRsrcID, nil);
- break;
-
- default:
- // DoDeskAcc (appleMenuRsrcID, itemNumber);
- SysBeep (1);
- break;
- }
- break;
-
- case fileMenuRsrcID:
- switch (itemNumber)
- {
- case itemQuit:
- gQuitApp = true;
- break;
-
- default:
- SysBeep (1);
- break;
- }
- break;
-
- case animationMenuRsrcID:
- switch (itemNumber)
- {
- case itemBegin:
- gDoAnimation = true;
- EnableItem (GetMHandle (windowMenuRsrcID), itemCycle);
- break;
-
- case itemEnd:
- gDoAnimation = false;
- DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
- break;
-
- case itemStep:
- MoveObject ();
- break;
- }
- break;
-
- case objectMenuRsrcID:
- switch (itemNumber)
- {
- case itemSquare:
- case itemOval:
- gIsSquare = (itemNumber == itemSquare);
- CheckItem (GetMHandle (menuID), itemNumber, true);
- CheckItem (GetMHandle (menuID), gIsSquare ? itemOval : itemSquare, false);
- InitializeViewPorts (gWindow);
- InvalRect (&gWindow->portRect);
- break;
-
- case itemDither:
- CheckItem (GetMHandle (menuID), itemNumber, gDoDither = !gDoDither);
- ChangeViewPorts (gWindow);
- InvalRect (&gWindow->portRect);
- break;
-
- case itemHalftone:
- CheckItem (GetMHandle (menuID), itemNumber, gDoHalftone = !gDoHalftone);
- ChangeViewPorts (gWindow);
- InvalRect (&gWindow->portRect);
- break;
- }
- break;
-
- case windowMenuRsrcID:
- if (itemNumber == itemShowFrame)
- CheckItem (GetMHandle (menuID), itemShowFrame, gShowFrame = ! gShowFrame);
- else if (itemNumber == itemCycle)
- {
- CheckItem (GetMHandle (menuID), itemCycle, gCyclePanes = ! gCyclePanes);
- if (gCyclePanes)
- GetNextCycle (&gNextCycle);
- }
- else
- {
- long oldItem;
-
- CheckItem (GetMHandle (menuID), itemNumber, true);
- oldItem = gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes;
- CheckItem (GetMHandle (menuID), oldItem, false);
- gPanesPerSide = itemNumber - itemMinimumPanes + kMinimumPanesPerSide;
- }
- ChangeViewPorts (gWindow);
- InvalRect (&gWindow->portRect);
- break;
-
- default:
- break;
- } /* switch (menuID) */
-
- HiliteMenu (kHiliteAllMenus);
-
- } /* DoMenuCommand */
-
-
- void
- DoGrowBox (EventRecord *pEvent, WindowPtr theWindow)
- {
- long newSize;
- Rect newRect,
- oldRect;
-
- oldRect = newRect = theWindow->portRect;
-
- if (newSize = GrowWindow (theWindow, pEvent->where, &qd.screenBits.bounds))
- {
- newRect.right = newRect.left + (short) newSize;
- newRect.bottom = newRect.top + (short) (newSize >> 16);
- SizeWindow (theWindow,
- newRect.right - newRect.left,
- newRect.bottom - newRect.top,
- true);
- InvalRect (&oldRect);
- ChangeViewPorts (gWindow);
- }
-
- } /* DoGrowBox */
-
-
- void
- DoZoomBox (EventRecord *pEvent, WindowPtr theWindow, short windowPart)
- {
- if (TrackBox (theWindow, pEvent->where, windowPart))
- {
- ZoomWindow (theWindow, windowPart, true);
- InvalRect (&theWindow->portRect);
- ChangeViewPorts (gWindow);
- }
-
- } /* DoZoomBox */
-
-
- void
- DoNullEvent (EventRecord *pEvent)
- {
- if (gDoAnimation)
- {
- if (gCyclePanes)
- {
- if (GetNextCycle (&gNextCycle))
- {
- gPanesPerSide = (gPanesPerSide == kMaximumPanesPerSide) ? kMinimumPanesPerSide
- : gPanesPerSide + 1;
- ChangeViewPorts (gWindow);
- InvalRect (&gWindow->portRect);
- }
- }
- MoveObject ();
- }
-
- } /* DoNullEvent */
-
-
- void
- DoMouseDown (EventRecord *pEvent)
- {
- short windowPart;
- WindowPtr whichWindow;
-
- windowPart = FindWindow (pEvent->where, &whichWindow);
-
- switch (windowPart)
- {
- case inDesk:
- break;
-
- case inMenuBar:
- DoMenuCommand (MenuSelect (pEvent->where));
- break;
-
- case inSysWindow:
- SystemClick (pEvent, whichWindow);
- break;
-
- case inContent:
- DoContentClick (pEvent, whichWindow);
- break;
-
- case inDrag:
- DragWindow (whichWindow, pEvent->where, &gDragRect);
- break;
-
- case inGrow:
- DoGrowBox (pEvent, whichWindow);
- break;
-
- case inGoAway:
- if (TrackGoAway (whichWindow, pEvent->where))
- {
- if (whichWindow == gWindow)
- gQuitApp = true;
- else
- DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- DoZoomBox (pEvent, whichWindow, windowPart);
- break;
-
- default:
- break;
- } /* switch */
-
- } /* DoMouseDown */
-
-
- void
- DoKeyStroke (EventRecord *pEvent)
- {
- char charCode;
-
- charCode = pEvent->message & charCodeMask;
-
- if (pEvent->modifiers & btnState)
- {
- /*--------------*/
- /* Button is UP */
- /*--------------*/
- if (pEvent->modifiers & cmdKey)
- {
- /*-------------------*/
- /* Command key x100 */
- /*-------------------*/
- DoMenuCommand (MenuKey (charCode));
- }
- }
-
- } /* DoKeyStroke */
-
-
- void
- DoUpdate (EventRecord *pEvent)
- {
- GrafPtr savedPort;
- WindowPtr pUpdateWindow;
-
- GetPort (&savedPort);
-
- pUpdateWindow = (WindowPtr) pEvent->message;
-
- SetPort (pUpdateWindow);
- BeginUpdate (pUpdateWindow);
-
- EraseRgn (pUpdateWindow->visRgn);
-
- if (pUpdateWindow == gWindow)
- {
- if (gShowFrame)
- DrawObjectFrame ();
- DrawObject ();
- }
-
- EndUpdate (pUpdateWindow);
- SetPort (savedPort);
-
- } /* DoUpdate */
-
-
- void
- DoActivate (EventRecord *pEvent)
- {
- WindowPtr pActiveWindow;
-
- /*--------------------------------------*/
- /* Get the window to de/activate */
- /* and its kind from the event message */
- /*--------------------------------------*/
- pActiveWindow = (WindowPtr) pEvent->message;
-
- SetPort (pActiveWindow);
-
- /*-----------------*/
- /* Activate window */
- /*-----------------*/
- if (pEvent->modifiers & activeFlag)
- {
- /* Enable/Disable items */
- }
- else
- /*-------------------*/
- /* Deactivate window */
- /*-------------------*/
- {
- /* Enable/Disable items */
- }
-
- } /* DoActivate */
-
-
- void
- DoEvent (void)
- {
- EventRecord theEvent;
-
- SystemTask (); /* Handle desk accessories */
-
- GetNextEvent (everyEvent, &theEvent);
-
- switch (theEvent.what)
- {
- case nullEvent:
- DoNullEvent (&theEvent);
- break;
-
- case mouseDown:
- DoMouseDown (&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- DoKeyStroke (&theEvent);
- break;
-
- case updateEvt:
- DoUpdate (&theEvent);
- break;
-
- case activateEvt:
- DoActivate (&theEvent);
- break;
-
- case mouseUp:
- case keyUp:
- case diskEvt:
- case networkEvt:
- case driverEvt:
- default:
- break;
- } /* switch (theEvent.what) */
-
- } /* DoEvent */
-
-
- main (void)
- {
- gQuitApp = false;
- if (InitApp ())
- {
- InitializeViewPortGlobals ();
-
- InitializeMenus ();
- if (gWindow = InitializeWindow ())
- {
- InitializeViewPorts (gWindow);
-
- while (! gQuitApp)
- DoEvent ();
-
- DisposeViewPorts (gWindow);
- }
- ExitApp ();
- }
-
- } /* main */
-